/**
 iTextUploader
 (C) 2008 MyselftDev (myselftdev@gmail.com)
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; version 2
 of the License.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/* Revision: $Id: main.m 120 2009-04-27 20:11:16Z myselftdev $ */

#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>

#import <sys/stat.h>

#import "AppController.h"

int main(int argc, char *argv[])
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	// Redirects NSLog() output to a file
	umask(022);
	FILE *newStderr = freopen(LOG_FILE, "w", stderr);
	
	if(checkLock())
	{
		NSLog(@"iTextUploader is running. Closing iTextUploader Lite.");
	}
	else
	{
		BOOL automatic = NO;
		
		if(argc > 1)
		{
			if(!strcmp(argv[1],"auto"))
			{
				NSLog(@"Executed by automatic upload");
				automatic = YES;
			}
		}
		
		// Write PID into lock file
		[[NSString stringWithFormat:@"%d", getpid(), nil] writeToFile:[@LOCK_FILE stringByExpandingTildeInPath] atomically:NO];
		
		// The real application starts here
		AppController *app = [[AppController alloc] init];
		[app startRequestAutomatic:automatic];
		[app release];
		
		// Remove locks
		[@"" writeToFile:[@LOCK_FILE stringByExpandingTildeInPath] atomically:NO];
	}
	
	NSLog(@"Quitting");
	
	[pool drain];
	
	return 0;
}
